import java.io.EOFException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RegenThread extends Thread {
	String query;
	float strkoef1, strkoef2, intkoef1, intkoef2, agikoef1, agikoef2, hpkoef,
			manakoef, strstaminakoef, intstaminakoef, agistaminakoef,
			strstaminaregen, intstaminaregen, agistaminaregen;
	int interval;
	Connection con;
	DatabaseConnect dc;
	Shalatik bot;
	boolean running = true;
	String channel;

	String prevnick = null;
	String prevword = null;
	Long prevtime = null;

	public RegenThread(Connection con, Shalatik bot, String channel,
			float strkoef1, float strkoef2, float intkoef1, float intkoef2,
			float agikoef1, float agikoef2, float hpkoef, float manakoef,
			int interval, float strstaminakoef, float intstaminakoef,
			float agistaminakoef, float strstaminaregen, float intstaminaregen, float agistaminaregen) {
		this.con = con;
		this.bot = bot;
		this.channel = channel;
		this.strkoef1 = strkoef1;
		this.strkoef2 = strkoef2;
		this.intkoef1 = intkoef1;
		this.intkoef2 = intkoef2;
		this.agikoef1 = agikoef1;
		this.agikoef2 = agikoef2;
		this.hpkoef = hpkoef;
		this.manakoef = manakoef;
		this.interval = interval;
		this.agistaminakoef = agistaminakoef;
		this.strstaminakoef = strstaminakoef;
		this.intstaminakoef = intstaminakoef;
		this.strstaminaregen = strstaminaregen;
		this.intstaminaregen = intstaminaregen;
		this.agistaminaregen = agistaminaregen;
	}

	boolean checkSpam(ResultSet rs) {
		return true;
	}

	@Override
	public synchronized void run() {
		try {
			dc = new DatabaseConnect(con);
			while (running) {
				wait(interval);
				float adendum;
				System.out.println("[debug] ceking regen");
				query = "SELECT * FROM irc_qog";
				ResultSet rs = dc.sendQuery(query);
				while (rs.next()) {
					int level = rs.getInt("level");
					String user = rs.getString("nick");
					String klass = rs.getString("class");
					int hp = rs.getInt("hp");
					int mana = rs.getInt("mana");
					int stamina = rs.getInt("stamina");
					if (klass.equals("agi"))
					{
						float maxHP = agikoef1 * hpkoef * level;
						float maxMana = agikoef2 * manakoef * level;
						float maxStamina = agistaminakoef * level;
						float addHP = agikoef1 * level;
						float addMana = agikoef2 * level;
						float addStamina = agistaminaregen;
						
						if (hp < maxHP && hp > 0)
						{
							query = "UPDATE irc_qog SET hp = LEAST(" + maxHP + ", hp + " + addHP + ")" +
									" WHERE nick = '" + user + "'";
							dc.sendUpdateQuery(query);
						}
						if (mana < maxMana)
						{
							query = "UPDATE irc_qog SET mana = LEAST(" + maxMana + ", mana + " + addMana + ")" +
							" WHERE nick = '" + user + "'";
							dc.sendUpdateQuery(query);
						}
						if (stamina < maxStamina)
						{
							query = "UPDATE irc_qog SET stamina = LEAST(" + maxStamina + ", stamina + " + addStamina + ")" +
							" WHERE nick = '" + user + "'";
							dc.sendUpdateQuery(query);
						}

					}
					if (klass.equals("int"))
					{
						float maxHP = intkoef1 * hpkoef * level;
						float maxMana = intkoef2 * manakoef * level;
						float maxStamina = intstaminakoef * level;
						float addHP = intkoef1 * level;
						float addMana = intkoef2 * level;
						float addStamina = intstaminaregen;
						
						if (hp < maxHP && hp > 0)
						{
							query = "UPDATE irc_qog SET hp = LEAST(" + maxHP + ", hp + " + addHP + ")" +
									" WHERE nick = '" + user + "'";
							dc.sendUpdateQuery(query);
						}
						if (mana < maxMana)
						{
							query = "UPDATE irc_qog SET mana = LEAST(" + maxMana + ", mana + " + addMana + ")" +
							" WHERE nick = '" + user + "'";
							dc.sendUpdateQuery(query);
						}
						if (stamina < maxStamina)
						{
							query = "UPDATE irc_qog SET stamina = LEAST(" + maxStamina + ", stamina + " + addStamina + ")" +
							" WHERE nick = '" + user + "'";
							dc.sendUpdateQuery(query);
						}
					}
					if (klass.equals("str"))
					{
						float maxHP = strkoef1 * hpkoef * level;
						float maxMana = strkoef2 * manakoef * level;
						float maxStamina = strstaminakoef * level;
						float addHP = strkoef1 * level;
						float addMana = strkoef2 * level;
						float addStamina = strstaminaregen;
						
						if (hp < maxHP && hp > 0)
						{
							query = "UPDATE irc_qog SET hp = LEAST(" + maxHP + ", hp + " + addHP + ")" +
									" WHERE nick = '" + user + "'";
							dc.sendUpdateQuery(query);
						}
						if (mana < maxMana)
						{
							query = "UPDATE irc_qog SET mana = LEAST(" + maxMana + ", mana + " + addMana + ")" +
									" WHERE nick = '" + user + "'";
							dc.sendUpdateQuery(query);
						}
						if (stamina < maxStamina)
						{
							query = "UPDATE irc_qog SET stamina = LEAST(" + maxStamina + ", stamina + " + addStamina + ")" +
									" WHERE nick = '" + user + "'";
							System.out.println(query);
							dc.sendUpdateQuery(query);
						}
					}
				}
				rs.close();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (EOFException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println(query);
			e.printStackTrace();
		}
	}

	public void turnOff() {
		running = false;
	}
}
